<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .ul1 {
            width: 300px;
            height: 50px;
            padding: 0;
            background-color: pink;
            list-style: none;
            display: flex;
            justify-content: space-evenly;
            align-items: center;
        }

        .ul1>li {
            width: 40px;
            height: 50px;
            line-height: 50px;
            text-align: center;
        }

        .ul2 {
            width: 300px;
            height: 300px;
            list-style: none;
            padding: 0;
            margin: 0;
            overflow: hidden;
        }


        .ul2>li {
            width: 300px;
            height: 300px;
            background-color: rgb(153, 109, 109);
            border: 1px solid #000;
            color: #fff;
            text-align: center;
            line-height: 300px;
            display: none;
        }
    </style>
</head>

<body>
    <ul class="ul1">
        <li class="li1">首页</li>
        <li class="li1">动漫</li>
        <li class="li1">游戏</li>
        <li class="li1">直播</li>
    </ul>
    <ul class="ul2">
        <li class="li2" style="display: block;">1</li>
        <li class="li2">2</li>
        <li class="li2">3</li>
        <li class="li2">4</li>
    </ul>
    <script>
        // 获取Dom
        const l1 = document.querySelectorAll('.li1')
        const l2 = document.querySelectorAll('.li2')
        console.log(l1);
        // 循环遍历每一项li标签
        for (let i = 0; i < l1.length; i++) {
            // 元素下标的设置
            l1[i].index = i
           
            //    给每一项绑定鼠标经过事件
            l1[i].addEventListener('mouseover', function () {
                this.style.backgroundColor = 'red'
                // 先将img全部隐藏
                for(let j = 0;j <l2.length;j++){
                 l2[j].style.display = 'none'
                }
                l2[this.index].style.display = 'block'

            })
            //    给每一项绑定鼠标离开事件
            l1[i].addEventListener('mouseout', function () {
                this.style.backgroundColor = 'pink'
            })
        }
    </script>
</body>

</html>